<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<div th:replace="common/common::html"></div>
<head>
    <meta charset="UTF-8">
    <title th:text="#{role.title.tip}">Title</title>
    <script th:inline="javascript">
        var _gridWidth;
        var _gridHeight;
        //页面自适应
        function resizePageSize(){
            _gridWidth = $(document).width()-12;
            _gridHeight = $(document).height()-110;
        }

        $(function () {
            resizePageSize();
            var _columnWidth = (_gridWidth-150)/3;
            $("#flexigrid-id").flexigrid({
                width : _gridWidth,
                height : _gridHeight,
                url : /*[[@{/role/queryRoleListByPage}]]*/"",
                dataType : 'json',
                colModel : [

                    {display : 'roleUuid',name : 'roleUuid',width : 150,sortable : false,align : 'center',hide : 'true'},
                    {display : /*[[#{role.name}]]*/"",name : 'roleName',width : _columnWidth, sortable : true,align : 'center'},
                    {display : /*[[#{role.remarks}]]*/"",name : 'remarks',width : _columnWidth, sortable : true,align : 'center'},
                    {display : /*[[#{common.operation}]]*/"",name : 'roleUuid',width : _columnWidth, sortable : false,align : 'center',
                        process:function (v,trid,rowData) {
                            if([[${#authorization.expression('hasRole("ROLE_cldk_data_role_menu")')}]]) {
                                var htmlContents = ''
                                htmlContents += ' <a href="#" title=[[#{role.menu.allocate}]] onclick="loadSingleZtree(\'' + v + '\')">'
                                htmlContents += '<i class="glyphicon glyphicon-wrench"></i>'
                                htmlContents += '</a>'
                            }
                            if([[${#authorization.expression('hasRole("ROLE_cldk_data_role_del")')}]]) {
                                htmlContents += ' <a href="#" title=[[#{common.delete}]] onclick="deleteRoleSingle(\'' + v + '\',\'' + rowData.roleName + '\')">'
                                htmlContents += '    <i class="glyphicon glyphicon-minus-sign"></i>'
                                htmlContents += '</a>'
                            }
                            return htmlContents;
                        }}
                ],
                resizable : false, //resizable table是否可伸缩
                useRp : true,
                usepager : true, //是否分页
                autoload : false, //自动加载，即第一次发起ajax请求
                hideOnSubmit : true, //是否在回调时显示遮盖
                showcheckbox : true, //是否显示多选框
                rowhandler : rowDbclick, //是否启用行的扩展事情功能,在生成行时绑定事件，如双击，右键等
                rowbinddata : true
            });

            query();
            //搜索框
            $("#search-role-id").searchTools({
                colModel:[
                    {display:[[#{role.name}]],domId:'search-role-name'},
                ],
                reback:function () {
                    query()
                }
            })
            //搜索按钮
            $("#query-btn-id").click(function () {
                query()
            })
            //分配权限对话框
            $("#allow-auth-dialog-id").dialog({
                autoOpen : false,
                width : 600,
                modal : true,
                resizable : false,
                title: /*[[#{role.menu.allocate.dialog}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function() {
                            var roleUuids = $("#allow-auth-dialog-id").attr("roleUuids");
                            var menuIds = []
                            var ztreeObject =  $.fn.zTree.getZTreeObj("allow-auth-ztree-id")
                            var checkedNodes = ztreeObject.getCheckedNodes(true)
                            $(checkedNodes).each(function (index,item) {
                                menuIds.push(item.id)
                            })
                            $.ajax({
                                type: 'post',
                                async: false,
                                dataType : 'json',
                                url: /*[[@{/role/saveMenuRefRole}]]*/'',
                                data: [
                                    {name:"roleUuids",value:roleUuids},
                                    {name:"menuIds",value:menuIds},
                                ],
                                success: function (data) {
                                    message(data.msg)
                                    if(data.success){
                                        $("#allow-auth-dialog-id").dialog('close')
                                    }

                                },
                                error: function(msg){
                                    message(/*[[#{common.error}]]*/'');
                                }

                            });

                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                ]
            });
            //批量分配权限按钮
            $("#allocate-auth-btn-id").click(function () {
                var ids = $("#flexigrid-id").getFlexigridiAttrByIndex(0);
                if(ids.length === 0 ){
                    message([[#{common.select}]])
                    return
                }
                loadSingleZtree(ids);

            })

            //增加角色按钮
            $("#add-btn-id").click(function () {
                $("#add-role-form-id").resetForm();
                $("#add-role-form-id .form-control").removeClass("ui-state-error")
                $("#add-role-dialog-id").dialog('open')
            })
            //增加角色对话框
            $("#add-role-dialog-id").dialog({
                autoOpen : false,
                width : 600,
                modal : true,
                resizable : false,
                title: /*[[#{role.add.dialog}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function() {
                            var flag = $("#add-role-form-id .form-control").checkDom()
                            if(!flag){
                                return;
                            }

                            $("#add-role-form-id").submit()

                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                ]
            });
            //增加角色form
            $("#add-role-form-id").ajaxForm({
                dataType: "json",
                success : function(data) {

                    message(data.msg)
                    if(data.success){
                        $("#add-role-dialog-id").dialog('close')
                        query();
                    }
                },
                error : function() {
                    message(/*[[#{common.error}]]*/'');
                }
            })
            //修改角色对话框
            $("#modify-role-dialog-id").dialog({
                autoOpen : false,
                width : 600,
                modal : true,
                resizable : false,
                title: /*[[#{role.modify.dialog}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function() {
                            var flag = $("#modify-role-form-id .form-control").checkDom()
                            if(!flag){
                                return;
                            }

                            $("#modify-role-form-id").submit()

                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                ]
            });
            //修改角色表单
            $("#modify-role-form-id").ajaxForm({
                dataType: "json",
                success : function(data) {

                    message(data.msg)
                    if(data.success){
                        $("#modify-role-dialog-id").dialog('close')
                        query();
                    }
                },
                error : function() {
                    message(/*[[#{common.error}]]*/'');
                }
            })
            //删除角色对话框
            $("#delete-role-dialog-id").dialog({
                autoOpen : false,
                width : 600,
                modal : true,
                resizable : false,
                title: /*[[#{role.delete.dialog}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function() {
                            var roleUuidArray = $("#delete-role-dialog-id").attr("roleUuidArray")
                            $.ajax({
                                type: 'post',
                                async: false,
                                dataType : 'json',
                                url: /*[[@{/role/deleteRoleById}]]*/'',
                                data: [{name:"roleUuids",value:roleUuidArray}],
                                success: function (data) {
                                    message(data.msg)
                                    if(data.success){
                                        query()
                                        $("#delete-role-dialog-id").dialog("close")
                                    }
                                },
                                error: function(msg){
                                    message(/*[[#{common.error}]]*/'');
                                }

                            });
                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                ]
            });
            //批量删除角色按钮
            $("#delete-btn-id").click(function () {
                var ids = $("#flexigrid-id").getFlexigridiAttrByIndex(0)
                if(ids.length === 0 ){
                    message([[#{common.select}]])
                    return
                }

                var roleNameArray = $("#flexigrid-id").getFlexigridiAttrByIndex(1)
                var roleNamesStr = ""
                $(roleNameArray).each(function (index,item) {
                    roleNamesStr+= item + ","
                })
                if(roleNamesStr !== ''){
                    roleNamesStr = roleNamesStr.substr(0,roleNamesStr.length - 1)
                }

                $("#delete-role-dialog-id").attr("roleUuidArray",ids)
                $("#delete-role-tip-id").text(roleNamesStr)
                $("#delete-role-dialog-id").dialog('open')
            })


        });//ready function

        //查询角色
        function query() {
            var roleName = $("#search-role-name").attr("select-value")
            $("#flexigrid-id").flexOptions({
                extParam:[
                    {name:"roleName",value:roleName}
                ]
            }).flexReload();
        }
        //双击事件
        function rowDbclick(r) {
            $(r).dblclick(function () {
                var columnArray = $(r).attr('ch').split('_FG$SP_');
                // console.info("row:" +　JSON.stringify(columnArray) )
                $("#mod-role-uuid").val(columnArray[0])
                $("#role-name-id").val(columnArray[1]);
                $("#role-remarks-id").val(columnArray[2]);
                $("#modify-role-dialog-id").dialog('open')
            });

        }

        var allocateSetting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        //单个分配权限
        function loadSingleZtree(roleUuid) {
            $("#allow-auth-dialog-id").attr("roleUuids",roleUuid)
            $.ajax({
                type: 'post',
                async: false,
                dataType : 'json',
                url: /*[[@{/role/queryMenuByRoleId}]]*/'',
                data: [{name:"roleUuid",value:roleUuid}],
                success: function (data) {
                    $.fn.zTree.init($("#allow-auth-ztree-id"), allocateSetting, data);
                    $("#allow-auth-dialog-id").dialog('open')
                },
                error: function(msg){
                    message(/*[[#{common.error}]]*/'');
                }

            });
        }
        //删除角色
        function deleteRoleSingle(roleUuid,roleName) {
            // console.info(`roleUuid:${roleUuid} roleName:${roleName}`)
            $("#delete-role-dialog-id").attr("roleUuidArray",roleUuid)
            $("#delete-role-tip-id").text(roleName)
            $("#delete-role-dialog-id").dialog('open')
        }
    </script>

    <style>
        header{
            display: flex;
            align-items: center;
            height: 44px;
        }

        header > nav,
        header > a{
            margin-right: 10px;
        }
        .cbit-grid div.bDiv table tr td div a:VISITED{
            color: #0192d1;
        }

        .form-group{
            margin-right: 10px;
        }

    </style>
</head>
<body>
<header>
    <nav sec:authorize="hasRole('ROLE_cldk_data_role_query')" id="search-role-id"></nav>

    <a sec:authorize="hasRole('ROLE_cldk_data_role_query')" id="query-btn-id" href="#" th:title="#{common.query}">
        <i class="glyphicon glyphicon-search"></i>
    </a>

    <a sec:authorize="hasRole('ROLE_cldk_data_role_menu')" id="allocate-auth-btn-id" href="#" th:title="#{role.menu.allocate}">
        <i class="glyphicon glyphicon-wrench"></i>
    </a>

    <a sec:authorize="hasRole('ROLE_cldk_data_role_add')" id="add-btn-id" href="#" th:title="#{common.add}">
        <i class="glyphicon  glyphicon-plus-sign"></i>
    </a>

    <a sec:authorize="hasRole('ROLE_cldk_data_role_del')" id="delete-btn-id" href="#" th:title="#{common.delete}">
        <i class="glyphicon  glyphicon glyphicon-minus-sign"></i>
    </a>
</header>
<table sec:authorize="hasRole('ROLE_cldk_data_role_query')" id="flexigrid-id"></table>

<div sec:authorize="hasRole('ROLE_cldk_data_role_menu')" id="allow-auth-dialog-id" style="display: none">
    <ul id="allow-auth-ztree-id" class="ztree"></ul>
</div>

<div id="add-role-dialog-id" style="display: none" sec:authorize="hasRole('ROLE_cldk_data_role_add')">
    <form id="add-role-form-id" action="addRoleInfo" method="post">
        <div class="form-group">
            <span th:text="#{role.name}"></span>
            <input name="roleName" th:placeholder="#{role.name.plchldr}" class="form-control" pattern="^.{4,}$">
        </div>

        <div class="form-group">
            <span th:text="#{role.remarks}"></span>
            <input name="remarks" th:placeholder="#{role.remarks.plchldr}" class="form-control" pattern="^.{4,}$">
        </div>
    </form>
</div>


<div id="modify-role-dialog-id" style="display: none" sec:authorize="hasRole('ROLE_cldk_data_role_mod')">
    <form id="modify-role-form-id" action="modifyRoleInfo" method="post">
        <input name="roleUuid" id="mod-role-uuid" type="hidden">
        <div class="form-group">
            <span th:text="#{role.name}"></span>
            <input id="role-name-id" name="roleName" th:placeholder="#{role.name.plchldr}" class="form-control" pattern="^.{2,}$">
        </div>

        <div class="form-group">
            <span th:text="#{role.remarks}"></span>
            <input id="role-remarks-id" name="remarks" th:placeholder="#{role.remarks.plchldr}" class="form-control" pattern="^.{2,}$">
        </div>
    </form>
</div>


<div sec:authorize="hasRole('ROLE_cldk_data_role_del')" id='delete-role-dialog-id' style="display: none">
    <span id="delete-role-tip-id"></span>
</div>

</body>
</html>